﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Text.RegularExpressions;

namespace Enterprise.Management.Common
{
    public class Utility
    {
        public static string GetRandomCode(int num)
        {
            Random r = new Random(Guid.NewGuid().GetHashCode());
            char[] words = {'0','1','2','3','4','5','6','7','8','9',
                'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};

            StringBuilder code = new StringBuilder();
            for (int i = 0; i < num; i++)
            {
                code.Append(words[r.Next(words.Length)]);
            }

            return code.ToString();
        }

        public static bool CheckIsMailFormat(string strValue)
        {
            return Utility.CheckIsFormat(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$", strValue);
        }

        public static bool CheckIsMailFormatEx(string strValue)
        {
            return Utility.CheckIsFormat(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", strValue);
        }

        public static bool CheckIsFormat(string strRegex, string strValue)
        {
            if (strValue != null && strValue.Trim() != "")
            {
                Regex re = new Regex(strRegex);
                if (re.IsMatch(strValue))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            return false;
        }
    }
}